<template>
  <div class="app-container">
    <!-- <div class="activity">
      <div v-for="(item, index) in activeData" :key="index" @click="handleClick(item)"
        style="width: 62px; height: 65px; margin-left: 15px; cursor: pointer">
        <img style="width: 48px; height: 48px; margin-left: 7px;border-radius: 6px;" :src="item.activityImg" />
        <div style="
            font-size: 12px;
            color: #666;
            text-align: center;
            border-radius: 6px;
          ">
          {{ item.activityName }}
        </div>
      </div>
    </div> -->
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
      label-width="80px">
      <el-row>
        <!-- <el-form-item label="活动类型" prop="activityType">
          <el-select v-model="queryParams.activityType" placeholder="请选择活动类型" clearable size="small">
            <el-option v-for="dict in activityTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="活动名称" prop="activityName">
          <el-input v-model="queryParams.activityName" placeholder="请输入活动名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="状态" prop="dataStatus">
          <el-select v-model="queryParams.dataStatus" placeholder="请选择状态" clearable size="small">
            <el-option v-for="dict in dataStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="活动时间" prop="activityTimeArray">
          <el-date-picker size="small" v-model="queryParams.activityTimeArray" type="daterange" align="right" clearable style="width: 328px"
            value-format="yyyy-MM-dd" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item style="margin-left: 22px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row class="row-mbinfo" style="margin-left:0;">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleClick"
          v-hasPermi="['activity:gift:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['activity:info:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['activity:info:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['activity:info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="divTable">
      <el-table border v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="ID" align="center" prop="activityUid"  /> -->
        <el-table-column label="活动类型" align="center" prop="activityType" :formatter="activityTypeFormat" />
        <el-table-column label="活动名称" align="left" prop="activityName" :show-overflow-tooltip="true" width="150">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleShowDetail(scope.row)">{{ scope.row.activityName }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="预热时间" align="center" prop="preHeatTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{
            parseTime(scope.row.preHeatTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
          </template>
        </el-table-column>
        <el-table-column label="开始时间" align="center" prop="beginTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{
            parseTime(scope.row.beginTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center" prop="endTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{
            parseTime(scope.row.endTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="规则简述" align="center" prop="ruleDesc" :show-overflow-tooltip="true" />
        <el-table-column label="详细描述" align="center" prop="detailedDesc" :show-overflow-tooltip="true" /> -->
        <!-- <el-table-column label="套餐价格" align="center" prop="packagesPrice" :show-overflow-tooltip="true"  />
      <el-table-column label="套餐图片" align="center" prop="packagesUrl" :show-overflow-tooltip="true"  /> -->
        <el-table-column label="预留支付时长" align="center" prop="payMinute" width="120" />
        <!-- <el-table-column label="是否自动拼团成功" align="center" prop="autoSuccess" :formatter="autoSuccessFormat"  /> -->
        <el-table-column label="状态" align="center" prop="dataStatus" :formatter="dataStatusFormat">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.dataStatus" active-value="1" inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <!-- <el-table-column label="排序" align="center" prop="dataPriority" :show-overflow-tooltip="true"  /> -->
        <el-table-column label="更新人" align="center" prop="updateName" width="130" :show-overflow-tooltip="true" />
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{
            parseTime(scope.row.updateTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="130" fixed="right">
          <template slot-scope="scope">
            <!-- <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)"
            v-hasPermi="['activity:info:query']">查看</el-button> -->
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['activity:info:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['activity:info:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改活动基础信息对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="平台ID" prop="platformUid">
          <el-input v-model="form.platformUid" placeholder="请输入平台ID" />
        </el-form-item>
        <el-form-item label="活动类型" prop="activityType">
          <el-select v-model="form.activityType" placeholder="请选择活动类型">
            <el-option v-for="dict in activityTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动名称" prop="activityName">
          <el-input v-model="form.activityName" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker clearable size="small" v-model="form.beginTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small" v-model="form.endTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="规则简述" prop="ruleDesc">
          <el-input v-model="form.ruleDesc" placeholder="请输入规则简述" />
        </el-form-item>
        <el-form-item label="详细描述" prop="detailedDesc">
          <el-input v-model="form.detailedDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="套餐价格" prop="packagesPrice">
          <el-input v-model="form.packagesPrice" placeholder="请输入套餐价格" />
        </el-form-item>
        <el-form-item label="套餐图片" prop="packagesUrl">
          <el-input v-model="form.packagesUrl" placeholder="请输入套餐图片" />
        </el-form-item>
        <el-form-item label="预留支付时长" prop="payMinute">
          <el-input v-model="form.payMinute" placeholder="请输入预留支付时长" />
        </el-form-item>
        <el-form-item label="自动拼团成功" prop="autoSuccess">
          <el-input v-model="form.autoSuccess" placeholder="请输入是否自动拼团成功" />
        </el-form-item>
        <el-form-item label="状态" prop="dataStatus">
          <el-select v-model="form.dataStatus" placeholder="请选择状态">
            <el-option v-for="dict in dataStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="dataPriority">
          <el-input v-model="form.dataPriority" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="排序" prop="updateUid">
          <el-input v-model="form.updateUid" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="更新人" prop="updateName">
          <el-input v-model="form.updateName" placeholder="请输入更新人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->

    <!-- 查看活动基础信息对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="1000px" append-to-body>
      <activityView v-if="showData" :activityVo="showData"></activityView>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listInfo,
    getInfo,
    delInfo,
    addInfo,
    updateInfo,
    updateStatus,
  } from "@/api/activity/info";

  import activityView from "./view";

  export default {
    name: "Info",
    components: {
      activityView,
    },
    data() {
      return {
        isBargain: false,
        isSeckill: false,
        isLimittime: false,
        isGroup: false,
        isFree: false,
        isPackages: false,
        isScore: false,
        activeName: "bargain",
        payPatternUpdate: false,
        activeData: [{
            activityType: "bargain",
            activityName: "砍价",
            activityImg: "https://commimg.pddpic.com/mms_static/2019-12-18/189fd889-ebe3-493f-ba1c-514fc59ec44b.png",
          },
          {
            activityType: "seckill",
            activityName: "秒杀",
            activityImg: "https://commfile.pddpic.com/galerie-go/static/2021-01-25/2d433b4a-ed98-4775-8a63-95d2de1e04a8.gif?a=0.5707277555402355",
          },
          {
            activityType: "limittime",
            activityName: "限时",
            activityImg: "https://commimg.pddpic.com/mms_static/2019-12-18/43fa8366-56a2-4efe-ad36-938c7b6fe1e4.png",
          },
          {
            activityType: "group",
            activityName: "拼团",
            activityImg: "https://commimg.pddpic.com/mms_static/2019-12-18/b0a8b9e9-2812-4e90-bde6-7a7b930a94e6.png",
          },
          {
            activityType: "free",
            activityName: "新人免费领",
            activityImg: "https://commfile.pddpic.com/galerie-go/955bbecf-e3d0-4545-9361-8a4c6b904ec5.png.slim.png",
          },
          {
            activityType: "packages",
            activityName: "套餐",
            activityImg: "https://commimg.pddpic.com/mms_static/2020-04-07/d54b519e-08be-4c5e-b684-6387fc9c7cd6.png",
          },
          {
            activityType: "score",
            activityName: "积分兑换",
            activityImg: "https://commimg.pddpic.com/mms_static/2019-12-27/9a489b67-6709-428c-bfff-13bb69c7d999.png",
          },
        ],
        result: null,
        selectType: {
          activityType: "bargain",
          activityName: "砍价",
          activityImg: "https://commfile.pddpic.com/galerie-go/static/2021-01-28/72195ac2-f5b5-4d1c-888e-ef21ff18eeae.png",
        },
        pickerOptions: {
          shortcuts: [{
              text: "最近一周",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit("pick", [start, end]);
              },
            },
            {
              text: "最近一个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit("pick", [start, end]);
              },
            },
            {
              text: "最近三个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit("pick", [start, end]);
              },
            },
          ],
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 活动基础信息表格数据
        infoList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: {},
        // 活动类型字典
        activityTypeOptions: [],
        // 是否自动拼团成功字典
        autoSuccessOptions: [],
        // 状态字典
        dataStatusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          activityType: "limittime",
          activityName: null,
          beginTime: null,
          endTime: null,
          dataStatus: null,
          activityTimeArray: null,
        },
        form: {},
        // 表单校验
        rules: {},
      };
    },
    created() {
      const activityUid = this.$route.params && this.$route.params.activityUid;

      this.getList();
      this.getDicts("activity_activity_type").then((response) => {
        this.activityTypeOptions = response.data;
      });
      this.getDicts("activity_auto_success").then((response) => {
        this.autoSuccessOptions = response.data;
      });
      this.getDicts("activity_data_status").then((response) => {
        this.dataStatusOptions = response.data;
      });
    },
    //监听路由
    watch: {
      $route(to, from) {
    	//from 从哪来
    	//to //到哪去
    	let path = from.path;
    	console.log(path)
    	if ("/selectInfo/data/" == path) {
    	  //刷新页面
    	  this.getList();
    	} else {
    	  let index = path.lastIndexOf("/");
    	  if (index > -1) {
    		path = path.substr(0, index);
    		if ("/selectInfo/data/" == path) {
    		  //刷新页面
    		  this.getList();
    		}
    	  }
    	}
      }
    },
    methods: {
      handleClick() {
        this.$router.push({
          path: "/selectInfo/data/",
          query: {
            activityName: "限时",
            activityType: "limittime",
          },
        });
        // this.resets();
        // switch (activeName) {
        //   case "bargain":
        //     this.isBargain = true;
        //     this.selectType = { activityType: "bargain", activityName: "砍价" };
        //     break;
        //   case "seckill":
        //     this.isSeckill = true;
        //     this.selectType = { activityType: "seckill", activityName: "秒杀" };
        //     break;
        //   case "limittime":
        //     this.isLimittime = true;
        //     this.selectType = { activityType: "limittime", activityName: "限时" };
        //     break;
        //   case "group":
        //     this.isGroup = true;
        //     this.selectType = { activityType: "group", activityName: "拼团" };
        //     break;
        //   case "free":
        //     this.isFree = true;
        //     this.selectType = {
        //       activityType: "free",
        //       activityName: "新人免费领",
        //     };
        //     break;
        //   case "packages":
        //     this.isPackages = true;
        //     this.selectType = { activityType: "packages", activityName: "套餐" };
        //     break;
        //   case "score":
        //     this.isScore = true;
        //     this.selectType = { activityType: "score", activityName: "积分兑换" };
        //     break;
        // }
      },
      resets() {
        this.isBargain = false;
        this.isSeckill = false;
        this.isLimittime = false;
        this.isGroup = false;
        this.isFree = false;
        this.isPackages = false;
        this.isScore = false;
      },
      /** 查询活动基础信息列表 */
      getList() {
        this.loading = true;
        this.queryParams.activityType = "limittime";
        listInfo(this.queryParams).then((response) => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 活动类型字典翻译
      activityTypeFormat(row, column) {
        return this.selectDictLabel(this.activityTypeOptions, row.activityType);
      },
      // 是否自动拼团成功字典翻译
      autoSuccessFormat(row, column) {
        return this.selectDictLabel(this.autoSuccessOptions, row.autoSuccess);
      },
      // 状态字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          activityUid: null,
          platformUid: null,
          activityType: null,
          activityName: null,
          beginTime: null,
          endTime: null,
          ruleDesc: null,
          detailedDesc: null,
          packagesPrice: null,
          packagesUrl: null,
          payMinute: null,
          autoSuccess: null,
          dataStatus: null,
          dataPriority: null,
          updateUid: null,
          updateTime: null,
          updateName: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.activityTimeArray = null;
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.activityUid);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
        this.names = selection.map(item => item.activityName);
        //用于禁用删除
        if (this.multiple == false) {
          for (var i = 0; i < selection.length; i++) {
            var element = selection[i];
            if ("1" === element.dataStatus) {
              this.multiple = true;
              break;
            }
          }
        }
      },

      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        let activityUid = row.activityUid || this.ids;
        this.$router.push({
          path: "/selectInfo/view/",
          query: {
            activityUid: activityUid,
          },
        });
        // this.showData = null;
        // const activityUid = row.activityUid;
        // getInfo(activityUid).then((response) => {
        //   this.showData = response.data;
        //   this.viewOpen = true;
        //   this.viewTitle = "活动详情";
        // });
      },
      handCloseDetail() {
        this.viewOpen = false;
        this.showData = null;
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        let activityUid = row.activityUid || this.ids;
        this.$router.push({
          path: "/selectInfo/data/",
          query: {
            activityUid: activityUid,
          },
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.activityUid != null) {
              updateInfo(this.form).then((response) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addInfo(this.form).then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      // 状态修改
      handleStatusChange(row) {
        let text = row.dataStatus === "0" ? "下架" : "上架";
        this.$confirm(
            '确定要把活动名称为"' + row.activityName + '",设为' + text + "吗？",
            "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }
          )
          .then(function() {
            return updateStatus(row.activityUid, row.dataStatus);
          })
          .then(() => {
            this.msgSuccess("操作成功");
            this.getList();
          })
          .catch(function() {
            row.dataStatus = row.dataStatus === "0" ? "1" : "0";
          });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const activityUids = row.activityUid || this.ids;
        const names = row.activityUid || this.names;
        this.$confirm(
            '是否确认删除活动名称为"' + names + '"的数据项?',
            "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }
          )
          .then(function() {
            return delInfo(activityUids);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.queryParams.activityType = "limittime";
        this.download(
          "activity/info/export", {
            ...this.queryParams,
          },
          `activity_info.xlsx`
        );
      },
    },
  };
</script>
<style lang="scss" scoped>
  .el-form-item {
    margin-bottom: 15px;
  }

  .activity {
    width: 100%;
    height: 100%;
    display: flex;
    background-color: #fff;
    padding: 10px;
    border-radius: 6px;
    margin-bottom: 20px;
  }
</style>
